using NUnit.Framework;

namespace ebcpatterns.tests
{
    [TestFixture]
    public class SharedStateTests
    {
        private SharedState<string> sut;

        [SetUp]
        public void Setup() {
            sut = new SharedState<string>();
        }

        [Test]
        public void Data_is_null_after_construction() {
            Assert.That(sut.Read(), Is.Null);    
        }

        [Test]
        public void Data_is_stored_by_writing_it() {
            sut.Write("x");
            Assert.That(sut.data, Is.EqualTo("x"));
        }

        [Test]
        public void Stored_data_can_be_read() {
            sut.data = "x";
            Assert.That(sut.Read(), Is.EqualTo("x"));
        }

        [Test]
        public void Write_and_Read_in_action() {
            sut.Write("x");
            Assert.That(sut.Read(), Is.EqualTo("x"));
        }

        [Test]
        public void Write_raises_an_event() {
            int count = 0;
            sut.DataWritten += () => count++;
            sut.Write("x");

            Assert.That(count, Is.EqualTo(1));
        }
    }
}